window.fs = require("fs");
window.path = require("path");
window.os = require("os");
window.url = require("url");

window.electron = require("electron");

window.pinyin = remote.require("pinyin-pro").pinyin;
window.store = remote.require("./util/store");
window.nodeCache = remote.require("./util/cache");
window.httpRequest = remote.require("./util/http.request");
window.uploadFiles = remote.require("./util/upload.file");
window.validates = remote.require("./util/validates");
window.helper = remote.require("./util/helper");
window.logger = remote.require("./util/logs");
window.hashget = remote.require("./util/hashget");
window.globalAreaCode = remote.require("./util/areaCodeJson.json"); // 行政区划
window.orgTypeJson = remote.require("./util/org.type.json"); // 组织类型
window.nationJson = remote.require("./util/nation.json"); // 民族类型

window.baiduSDK = remote.require("./util/baidu");
window.tencentCloudSDK = remote.require("./util/tencentcloud");
window.aliyunSDK = remote.require("./util/aliyun");

window.currentWindow = remote.getCurrentWindow();

window.currentWindow.maximizable = false;
window.currentWindow.fullScreenable = false;
window.currentWindow.resizable = false;
window.currentWindow.unmaximize();
// window.currentWindow.restore();
window.currentWindow.show();
window.currentWindow.focus();
window.currentWindow.shadow = true;

let isCenter = nodeCache.get("mainWindowIsCenter");
if (!isCenter && process.env.NODE_ENV !== "development") {
    currentWindow.center();
    nodeCache.set("mainWindowIsCenter", true, 60 * 60);
}

Date.prototype.getUTCTime = function () {
    return this.getTime() + this.getTimezoneOffset() * 60 * 1000;
};

Date.prototype.UTCTimeToLocal = function (time) {
    return time - this.getTimezoneOffset() * 60 * 1000;
};

window.sleep = (time) => {
    return new Promise((resolve) => setTimeout(resolve, time));
};

if (!Array.prototype.GroupBy) {
    /**
     * 对对象数组进行分组,返回键值属性值对象
     * */
    Array.prototype.GroupBy = function (keys) {
        return this.reduce((group, record) => {
            let groupKeys = "";

            if (typeof keys == "string") {
                groupKeys = record[keys];
            } else if (typeof keys == "array") {
                keys.forEach((a) => {
                    groupKeys += record[a];
                });
            } else {
                console.warn("请输入字符串或字符串数组");
            }
            if (!group[groupKeys]) {
                group[groupKeys] = [];
            }
            group[groupKeys].push(record);

            return group;
        }, {});
    };
}

if (!Array.prototype.GroupMap) {
    /**
     * 对对象数组进行分组,返回键值属性值对象
     * */
    Array.prototype.GroupMap = function (keys) {
        let groupObject = this.reduce((group, record) => {
            let groupKeys = "";

            if (typeof keys == "string") {
                groupKeys = record[keys];
            } else if (typeof keys == "array") {
                keys.forEach((a) => {
                    groupKeys += record[a];
                });
            } else {
                console.warn("请输入字符串或字符串数组");
            }

            if (!group[groupKeys]) {
                group[groupKeys] = {
                    records: [],
                };
                if (typeof keys == "string") {
                    group[groupKeys][keys] = record[keys];
                } else if (typeof keys == "array") {
                    keys.forEach((a) => {
                        group[groupKeys][a] += record[a];
                    });
                } else {
                    console.warn("请输入字符串或字符串数组");
                }
            }
            group[groupKeys].records.push(record);

            return group;
        }, {});

        let mapResult = [];
        for (let item in groupObject) {
            mapResult.push(groupObject[item]);
        }
        return mapResult;
    };
}
